package cn.iocoder.yudao.module.blog.controller.app.jiutag;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.blog.controller.admin.jiutag.vo.BlogJiuTagCreateReqVO;
import cn.iocoder.yudao.module.blog.controller.admin.jiutag.vo.BlogJiuTagPageReqVO;
import cn.iocoder.yudao.module.blog.controller.admin.jiutag.vo.BlogJiuTagRespVO;
import cn.iocoder.yudao.module.blog.convert.jiutag.BlogJiuTagConvert;
import cn.iocoder.yudao.module.blog.dal.dataobject.jiutag.BlogJiuTagDO;
import cn.iocoder.yudao.module.blog.dal.dataobject.jiutagclassity.BlogJiuTagClassityDO;
import cn.iocoder.yudao.module.blog.service.jiutag.BlogJiuTagService;
import cn.iocoder.yudao.module.blog.service.jiutagclassity.BlogJiuTagClassityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP - 案例标签")
@RestController
@RequestMapping("/blog/tag")
@Validated
@Slf4j
public class AppBlogJiuTagController {
    @Resource
    private BlogJiuTagService jiuTagService;

    @Resource
    private BlogJiuTagClassityService blogJiuTagClassityService;



    @PostMapping("/create")
    @Operation(summary = "创建案例标签")
    public CommonResult<Long> createJiuTag(@Valid @RequestBody BlogJiuTagCreateReqVO createReqVO) {
        return success(jiuTagService.createJiuTag(createReqVO));
    }

    @GetMapping("/hot")
    @Operation(summary = "获取所有标签")
    public CommonResult<List<BlogJiuTagRespVO>> getHot() {
        List<BlogJiuTagDO> list = jiuTagService.getHotTags();

        Set<Long> classityIds = CollectionUtils.convertSet(list, BlogJiuTagDO::getClassity);
        List<BlogJiuTagClassityDO> classityList = blogJiuTagClassityService.getJiuTagClassityList(classityIds);
        Map<Long, BlogJiuTagClassityDO> classityMap = CollectionUtils.convertMap(classityList, BlogJiuTagClassityDO::getId);

        return success(BlogJiuTagConvert.INSTANCE.convertList(list, classityMap));
    }

    @GetMapping("/all")
    @Operation(summary = "获取所有标签")
    public CommonResult<List<BlogJiuTagRespVO>> getAll(){
        List<BlogJiuTagDO> list = jiuTagService.getAllTags();

        Set<Long> classityIds = CollectionUtils.convertSet(list, BlogJiuTagDO::getClassity);
        List<BlogJiuTagClassityDO> classityList = blogJiuTagClassityService.getJiuTagClassityList(classityIds);
        Map<Long, BlogJiuTagClassityDO> classityMap = CollectionUtils.convertMap(classityList, BlogJiuTagClassityDO::getId);

        return success(BlogJiuTagConvert.INSTANCE.convertList(list, classityMap));
    }

    @GetMapping("/page")
    @Operation(summary = "获得案例标签分类分页")
    public CommonResult<PageResult<BlogJiuTagRespVO>> getJiuTagPage(@Valid BlogJiuTagPageReqVO pageVO) {
        PageResult<BlogJiuTagDO> pageResult = jiuTagService.getJiuTagPage(pageVO);
        return success(BlogJiuTagConvert.INSTANCE.convertPage(pageResult));
    }
}
